<template>
  <div class="app-container">
    <el-card style="height:90vh">
      <div slot="header" class="clearfix">
        <span>编辑文章</span>
        <el-button style="float: right; padding: 3px 0" type="text" @click="publish">发布</el-button>
      </div>
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="文章标题">
          <el-input v-model="form.title"/>
        </el-form-item>
        <el-form-item label="文章来源">
          <el-input v-model="form.origin" />
        </el-form-item>
        <el-form-item label="封面图">
          <el-upload
            ref="upload"
            :action="action"
            :headers="{'authorization': 'Bearer ' + token}"
            list-type="picture-card"
            :before-upload="beforeAvatarUpload"
            :auto-upload="false"
            :data="{...form}"
            :limit="1"
            :on-preview="handlePictureCardPreview"
            :on-remove="handleRemove"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="">
          </el-dialog>
        </el-form-item>
        <el-form-item label="文章描述">
          <el-input v-model="form.description" type="textarea" />
        </el-form-item>
        <el-form-item label="文章内容">
          <quill-editor
            ref="myQuillEditor"
            v-model="form.content"
            style="height: 350px"
            class="editor"
            :options="editorOption"
            @blur="onEditorBlur($event)"
            @focus="onEditorFocus($event)"
            @change="onEditorChange($event)"
          />
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'

export default {
  name: 'Create',
  data() {
    return {
      dialogImageUrl: '',
      dialogVisible: false,
      disabled: false,
      form: {},
      content: null,
      editorOption: {
        placeholder: '请输入',
        theme: 'snow'
      },
      id: null,
      imageUrl: '',
      action: ''
    }
  },
  computed: {
    ...mapGetters(['token'])
  },
  created() {
    const id = this.$route.params.id
    this.getArticle(id)
    this.action = `${process.env.VUE_APP_BASE_API}/cms/article/update/${id}`
  },
  methods: {
    async publish() {
      this.$refs.upload.submit()
      this.form = {}
    },
    async getArticle(id) {
      const res = await this.$http.get(`/cms/article/findOne/${id}`)
      if (res.code === 0) {
        const { title, description, origin, content } = res.data
        this.form = { title, description, origin, content }
      }
    },

    onEditorBlur() {
    },

    onEditorFocus() {
    },

    onEditorChange() {
    },

    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg' || 'image/png'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    handleRemove(file, fileList) {
      console.log(file, fileList)
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    }
  }
}
</script>
<style lang="scss">
  .el-upload {
    border: 1px dashed #d9d9d9;
    /*position: relative;*/
    /*overflow: hidden;*/
    width: 78px;
    height: 78px;
    line-height: 78px;
  }
  .el-upload-list__item{
    width: 78px !important;
    height: 78px !important;
  }
</style>
